package com.xr.blog.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xr.blog.exception.BlogException;
import com.xr.blog.exception.CustomException;
import com.xr.blog.pojo.BlogArticle;
import com.xr.blog.pojo.BlogCategory;
import com.xr.blog.service.BlogCategoryService;
import com.xr.blog.service.BlogLinkService;
import com.xr.blog.service.BlogTagService;
import com.xr.blog.tools.constant.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 前台分类控制器
 * @author 幸仁杰
 */
@Controller
@RequestMapping("category")
public class CategoryController extends BaseController {

    @Autowired
    private BlogCategoryService categoryService;

    @Autowired
    private BlogLinkService linkService;

    @Autowired
    private BlogTagService tagService;
    /**
     * 分类集合页
     * @return
     */
    @GetMapping
    public String tags(Model model) {
        List<BlogCategory> categories=categoryService.list();
        model.addAttribute("list",categories);
        // 热门文章
        List<BlogArticle> hotArticles=articleService.findHotArticles(8);
        model.addAttribute("hotArticles", hotArticles);
        // 热门标签
        model.addAttribute("hotTags", tagService.findHotTags(20));
        // 友情链接
        model.addAttribute("friendLinks", linkService.findLinkByType(1));
        // 个人链接
        model.addAttribute("personalLinks", linkService.findLinkByType(2));
        return "categories";
    }

    /**
     * 获得指定分类下的文章
     * @return
     */
    @GetMapping("{url}")
    public String category(Model model, @PathVariable(value = "url") String url) {
        return category(model,url,1);
    }

    /**
     *
     * @param model
     * @param url
     * @param pageIndex 需要加载的页码
     * @return
     */
    @GetMapping("{url}/{pageIndex}")
    public String category(Model model, @PathVariable(value = "url") String url, @PathVariable(value = "pageIndex") Integer pageIndex) {
        BlogCategory category=categoryService.getOne(new QueryWrapper<BlogCategory>().eq("url",url));
        if(null==category){
            throw new BlogException(40004,"类别不存在："+url);
        }
        // 热门文章
        List<BlogArticle> hotArticles=articleService.findHotArticles(8);
        model.addAttribute("hotArticles", hotArticles);
        // 热门标签
        model.addAttribute("hotTags", tagService.findHotTags(20));
        // 友情链接
        model.addAttribute("friendLinks", linkService.findLinkByType(1));
        // 个人链接
        model.addAttribute("personalLinks", linkService.findLinkByType(2));
        IPage<BlogArticle> page=articleService.findPageByCategory(new Page<>(pageIndex,5),category.getId());
        model.addAttribute("info",category);
        model.addAttribute("page",page);
        //分页代码
        model.addAttribute("pageHtml",pagination((int)page.getCurrent(),(int)page.getPages(), "/category/"+url+"/"));
        return "list";
    }
}
